<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="扩展包开发" tabindex="-1"><a class="header-anchor" href="#扩展包开发"><span>扩展包开发</span></a></h2>
<ul>
<li><a href="#introduction">简介</a>
<ul>
<li><a href="#a-note-on-facades">Facades 注解</a></li>
</ul>
</li>
<li><a href="#package-discovery">发现扩展包</a></li>
<li><a href="#service-providers">服务提供者</a></li>
<li><a href="#resources">资源文件</a>
<ul>
<li><a href="#configuration">配置</a></li>
<li><a href="#migrations">数据库迁移</a></li>
<li><a href="#routes">路由</a></li>
<li><a href="#translations">语言包</a></li>
<li><a href="#views">视图</a></li>
</ul>
</li>
<li><a href="#commands">命令</a></li>
<li><a href="#public-assets">公共资源文件</a></li>
<li><a href="#publishing-file-groups">发布群组文件</a></li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p>扩展包是向 Laravel 中添加功能的主要方式。扩展包可以包含很多有用的功能，例如时间处理扩展包 <a href="https://github.com/briannesbitt/Carbon" target="_blank" rel="noopener noreferrer">Carbon</a>，亦或者是提供完整 BDD 测试框架的扩展包 <a href="https://github.com/Behat/Behat" target="_blank" rel="noopener noreferrer">Behat</a>。</p>
<p>当然，扩展包有很多种类型。有些扩展包是独立运行的，意味着他们可以在任意的 PHP 框架中使用。Carbon 和 Behat 就是这样的独立扩展包。要在 Laravel 中使用这种扩展包只需要在 <code v-pre>composer.json</code> 文件中引入他们即可。</p>
<p>另一方面，有些扩展包只能在 Laravel 中使用。这些扩展包可能包含专门用来增强 Laravel 应用的路由、控制器、视图和配置的文件。这份指南主要介绍 Laravel 扩展包的开发。</p>
<h3 id="facades-注解" tabindex="-1"><a class="header-anchor" href="#facades-注解"><span>Facades 注解</span></a></h3>
<p>当开发 Laravel 应用时，通常使用契约（contracts）或 facades 没有什么区别，因为他们都提供基本相同的可测试能力。然而，在进行扩展包开发的时候，扩展包并不能访问 Laravel 提供的所有测试辅助函数。如果你想像在 Laravel 应用中一样编写扩展包的测试用例，你可以使用扩展包 <a href="https://github.com/orchestral/testbench" target="_blank" rel="noopener noreferrer">Orchestral Testbench</a> 。</p>
<h2 id="发现扩展包" tabindex="-1"><a class="header-anchor" href="#发现扩展包"><span>发现扩展包</span></a></h2>
<p>在 Laravel 应用的 <code v-pre>config/app.php</code> 配置文件中，<code v-pre>providers</code> 选项定义了能够被 Laravel 加载的服务提供者列表。当有人安装你的扩展包时，你需要将你的服务提供者包含到这个列表中。你可以将服务提供者定义到扩展包的 <code v-pre>composer.json</code> 文件中的 <code v-pre>extra</code> 部分，而不是让用户手动将你的服务提供者添加到列表中。除了服务提供者，还可以列出所有你想注册的 <a href="https://learnku.com/docs/laravel/6.x/facades" target="_blank" rel="noopener noreferrer">facades</a> ：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string double-quoted-string">"extra"</span><span class="token punctuation">:</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token string double-quoted-string">"laravel"</span><span class="token punctuation">:</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token string double-quoted-string">"providers"</span><span class="token punctuation">:</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string double-quoted-string">"Barryvdh\\Debugbar\\ServiceProvider"</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string double-quoted-string">"aliases"</span><span class="token punctuation">:</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token string double-quoted-string">"Debugbar"</span><span class="token punctuation">:</span> <span class="token string double-quoted-string">"Barryvdh\\Debugbar\\Facade"</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>一旦你的扩展包配置为可发现，Laravel 就会在安装时自动注册扩展包的服务提供者和 facades，为扩展包的用户提供一个友好的安装体验。</p>
<h3 id="选择性的发现扩展包" tabindex="-1"><a class="header-anchor" href="#选择性的发现扩展包"><span>选择性的发现扩展包</span></a></h3>
<p>如果你是扩展包的用户，想要禁止一个扩展包被发现，你可以在应用的 <code v-pre>composer.json</code> 文件中的 <code v-pre>extra</code> 部分列出这个扩展包：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string double-quoted-string">"extra"</span><span class="token punctuation">:</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token string double-quoted-string">"laravel"</span><span class="token punctuation">:</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token string double-quoted-string">"dont-discover"</span><span class="token punctuation">:</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string double-quoted-string">"barryvdh/laravel-debugbar"</span></span>
<span class="line">        <span class="token punctuation">]</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以通过在应用的 <code v-pre>dont-discover</code> 指令中使用 <code v-pre>*</code> 字符，禁用扩展包发现功能：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string double-quoted-string">"extra"</span><span class="token punctuation">:</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token string double-quoted-string">"laravel"</span><span class="token punctuation">:</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token string double-quoted-string">"dont-discover"</span><span class="token punctuation">:</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string double-quoted-string">"*"</span></span>
<span class="line">        <span class="token punctuation">]</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="服务提供者" tabindex="-1"><a class="header-anchor" href="#服务提供者"><span>服务提供者</span></a></h2>
<p><a href="https://learnku.com/docs/laravel/6.x/providers" target="_blank" rel="noopener noreferrer">服务提供者</a> 让你的扩展包与 Laravel 联系在一起。服务提供者负责将一些东西绑定到 Laravel 的 <a href="https://learnku.com/docs/laravel/6.x/container" target="_blank" rel="noopener noreferrer">服务容器</a> 中，并且告诉 Laravel 从哪里加载扩展包的资源文件，例如视图、配置文件、语言包等。</p>
<p>服务提供者继承了 <code v-pre>Illuminate\Support\ServiceProvider</code> 类，并包含了两个方法：<code v-pre>register</code> 和 <code v-pre>boot</code>。基类 ServiceProvider 位于名为 <code v-pre>illuminate/support</code> 的 Composer 扩展包中，你必须将它加入到你的扩展包依赖。想要了解更多关于服务提供者的结构和用途，请查阅 <a href="https://learnku.com/docs/laravel/6.x/providers" target="_blank" rel="noopener noreferrer">它的文档</a>。</p>
<h2 id="资源文件" tabindex="-1"><a class="header-anchor" href="#资源文件"><span>资源文件</span></a></h2>
<h3 id="配置" tabindex="-1"><a class="header-anchor" href="#配置"><span>配置</span></a></h3>
<p>有时，你需要将扩展包配置文件发布到应用本身的 <code v-pre>config</code> 目录中。这样使用扩展包的用户就可以轻松的重写默认配置项。想要发布扩展包配置文件，只需要在服务提供者的 <code v-pre>boot</code> 方法中调用 <code v-pre>publishes</code> 方法即可：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 在注册后启动服务</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">publishes</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token constant">__DIR__</span><span class="token operator">.</span><span class="token string single-quoted-string">'/path/to/config/courier.php'</span> <span class="token operator">=></span> <span class="token function">config_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'courier.php'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>现在，当扩展包的用户执行 Laravel 的 <code v-pre>vendor:publish</code> 命令，扩展包文件就会被复制到指定的目录中。当然，一旦你的配置文件被发布，就可以如同其他配置一样被访问：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">config</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'courier.option'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p>{注意} 你不应该在配置文件中定义闭包函数。因为当用户执行 <code v-pre>config:cache</code> Artisan 命令时，配置文件将不能被正确的序列化。</p>
</blockquote>
<h4 id="扩展包默认配置" tabindex="-1"><a class="header-anchor" href="#扩展包默认配置"><span>扩展包默认配置</span></a></h4>
<p>你也可以将扩展包默认配置与应用的副本配置合并在一起。这样扩展包用户就可以在副本配置文件中定义他们想要覆盖的配置选项。想要合并配置，只需要在服务提供者的 <code v-pre>register</code> 方法中调用 <code v-pre>mergeConfigFrom</code> 方法即可：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> *  在容器中注册绑定</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">register</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">mergeConfigFrom</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token constant">__DIR__</span><span class="token operator">.</span><span class="token string single-quoted-string">'/path/to/config/courier.php'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'courier'</span></span>
<span class="line">    <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>{注意} 此方法只合并配置数组的第一维。如果扩展包用户定义了多维配置数组，缺少的选项将不会被合并。</p>
</blockquote>
<h3 id="路由" tabindex="-1"><a class="header-anchor" href="#路由"><span>路由</span></a></h3>
<p>如果你的扩展包中包含路由文件，你需要使用 <code v-pre>loadRoutesFrom</code> 方法加载他们。此方法将自动判断应用的路由是否已被缓存，如果路由已缓存，将不会加载你的路由文件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 在注册后启动服务</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">loadRoutesFrom</span><span class="token punctuation">(</span><span class="token constant">__DIR__</span><span class="token operator">.</span><span class="token string single-quoted-string">'/routes.php'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="数据库迁移" tabindex="-1"><a class="header-anchor" href="#数据库迁移"><span>数据库迁移</span></a></h3>
<p>如果你的扩展包中包含 <a href="https://learnku.com/docs/laravel/6.x/migrations" target="_blank" rel="noopener noreferrer">数据库迁移</a>，你需要使用 <code v-pre>loadMigrationsFrom</code> 方法告知 Laravel 如何加载他们。<code v-pre>loadMigrationsFrom</code> 方法只需要扩展包迁移文件路径作为唯一参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 在注册后启动服务</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">loadMigrationsFrom</span><span class="token punctuation">(</span><span class="token constant">__DIR__</span><span class="token operator">.</span><span class="token string single-quoted-string">'/path/to/migrations'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>一旦你的扩展包迁移文件被注册，当运行 <code v-pre>php artisan migrate</code> 命令时他们就会被自动执行。你不需要将他们导入到应用的 <code v-pre>database/migrations</code> 目录中。</p>
<h3 id="语言包" tabindex="-1"><a class="header-anchor" href="#语言包"><span>语言包</span></a></h3>
<p>如果你的扩展包中包含 <a href="https://learnku.com/docs/laravel/6.x/localization" target="_blank" rel="noopener noreferrer">语言包文件</a>，你需要使用 <code v-pre>loadTranslationsFrom</code> 方法告知 Laravel 如何加载他们。例如，如果你的扩展包名为 <code v-pre>courier</code>，你需要将下面的内容加入到服务提供者的 <code v-pre>boot</code> 方法中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 在注册后启动服务</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">loadTranslationsFrom</span><span class="token punctuation">(</span><span class="token constant">__DIR__</span><span class="token operator">.</span><span class="token string single-quoted-string">'/path/to/translations'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'courier'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>扩展包翻译约定使用 <code v-pre>package::file.line</code> 语法进行引用。因此，你可以按照下面的方式来加载 <code v-pre>courier</code> 扩展包中的 <code v-pre>messages</code> 文件的 <code v-pre>welcome</code> 行：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token function">trans</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'courier::messages.welcome'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="发布语言包" tabindex="-1"><a class="header-anchor" href="#发布语言包"><span>发布语言包</span></a></h4>
<p>如果你想要将扩展包中的语言包发布到应用的 <code v-pre>resources/lang/vendor</code> 目录，可以使用服务提供者的 <code v-pre>publishes</code> 方法。<code v-pre>publishes</code> 方法接收一个包含语言包路径和对应发布位置的数组。例如，发布 <code v-pre>courier</code> 扩展包的语言包文件，操作如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 在注册后启动服务</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">loadTranslationsFrom</span><span class="token punctuation">(</span><span class="token constant">__DIR__</span><span class="token operator">.</span><span class="token string single-quoted-string">'/path/to/translations'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'courier'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">publishes</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token constant">__DIR__</span><span class="token operator">.</span><span class="token string single-quoted-string">'/path/to/translations'</span> <span class="token operator">=></span> <span class="token function">resource_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'lang/vendor/courier'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>现在，当扩展包的用户执行 Laravel 的 <code v-pre>vendor:publish</code> Artisan 命令，语言包将会被发布到指定的目录中。</p>
<h3 id="视图" tabindex="-1"><a class="header-anchor" href="#视图"><span>视图</span></a></h3>
<p>想要在 Laravel 中注册你的扩展包的 <a href="https://learnku.com/docs/laravel/6.x/views" target="_blank" rel="noopener noreferrer">视图</a> ， 需要告知 Laravel 视图文件的位置。你可以使用服务提供者的 <code v-pre>loadViewsFrom</code> 方法来实现。<code v-pre>loadViewsFrom</code> 方法允许接收两个参数：视图模板路径和扩展包名。例如，如果你的扩展包名为 <code v-pre>courier</code>，你需要将下面的内容加入到服务提供者的 <code v-pre>boot</code> 方法中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 在注册后启动服务</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">loadViewsFrom</span><span class="token punctuation">(</span><span class="token constant">__DIR__</span><span class="token operator">.</span><span class="token string single-quoted-string">'/path/to/views'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'courier'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>扩展包视图约定使用 <code v-pre>package::view</code> 语法进行引用。因此，一旦视图路径在服务提供者中注册成功，你可以使用下面的方式来加载 <code v-pre>courier</code> 扩展包中的 <code v-pre>admin</code> 视图：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'admin'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'courier::admin'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="重写扩展包视图" tabindex="-1"><a class="header-anchor" href="#重写扩展包视图"><span>重写扩展包视图</span></a></h4>
<p>当你使用 <code v-pre>loadViewsFrom</code> 方法时，Laravel 实际上在两个位置注册视图：应用的 <code v-pre>resources/views/vendor</code> 目录和你的自定义目录。所以，还以 <code v-pre>courier</code> 扩展包为例，Laravel 首先会检查开发人员是否在 <code v-pre>resources/views/vendor/courier</code> 中提供了一个自定义版本的视图。然后，如果视图尚未被定义，Laravel 将会搜索在 <code v-pre>loadViewsFrom</code> 中定义的视图目录。这种方法可以让用户很简单的自定义或重写扩展包的视图。</p>
<h4 id="发布视图" tabindex="-1"><a class="header-anchor" href="#发布视图"><span>发布视图</span></a></h4>
<p>如果你希望将你的视图发布到应用的 <code v-pre>resources/views/vendor</code> 目录中，可以使用服务提供者的 <code v-pre>publishes</code> 方法。<code v-pre>publishes</code> 方法接收一个包含视图路径和对应发布位置的数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 在注册后启动服务</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">loadViewsFrom</span><span class="token punctuation">(</span><span class="token constant">__DIR__</span><span class="token operator">.</span><span class="token string single-quoted-string">'/path/to/views'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'courier'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">publishes</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token constant">__DIR__</span><span class="token operator">.</span><span class="token string single-quoted-string">'/path/to/views'</span> <span class="token operator">=></span> <span class="token function">resource_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'views/vendor/courier'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>现在，当扩展包的用户执行 Laravel 的 <code v-pre>vendor:publish</code> Artisan 命令，视图将会被发布到自定的目录中。</p>
<h2 id="命令" tabindex="-1"><a class="header-anchor" href="#命令"><span>命令</span></a></h2>
<p>想要在 Laravel 中注册扩展包的 Artisan 命令，需要使用 <code v-pre>commands</code> 方法。此方法接收一个命令类的数组。一旦这些命令注册成功，可以使用 <a href="https://learnku.com/docs/laravel/6.x/artisan" target="_blank" rel="noopener noreferrer">Artisan 命令行</a> 执行他们：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 在注册后启动服务</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">app</span><span class="token operator">-></span><span class="token function">runningInConsole</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">commands</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">            <span class="token class-name static-context">FooCommand</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token class-name static-context">BarCommand</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="公共资源文件" tabindex="-1"><a class="header-anchor" href="#公共资源文件"><span>公共资源文件</span></a></h2>
<p>你的扩展包中可能存在 JavaScript、CSS 和图片之类的资源文件。想要发布这些资源文件到应用的 <code v-pre>public</code> 目录，可以使用服务提供者的 <code v-pre>publishes</code> 方法。在下面的例子中，我们也可以添加一个 <code v-pre>public</code> 资源分类标签，可用于相关发布资源的分类：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 在注册后启动服务</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">publishes</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token constant">__DIR__</span><span class="token operator">.</span><span class="token string single-quoted-string">'/path/to/assets'</span> <span class="token operator">=></span> <span class="token function">public_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'vendor/courier'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'public'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在，当扩展包的用户执行 <code v-pre>vendor:publish</code> 命令，你的资源文件将会被复制到指定的目录中。由于每次更新扩展包时通常都需要覆盖资源文件，因此需要使用 <code v-pre>--force</code> 标签：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan vendor<span class="token punctuation">:</span>publish <span class="token operator">--</span>tag<span class="token operator">=</span><span class="token keyword">public</span> <span class="token operator">--</span>force</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="发布群组文件" tabindex="-1"><a class="header-anchor" href="#发布群组文件"><span>发布群组文件</span></a></h2>
<p>你可能想要分别打包发布扩展包资源文件或资源。举个例子，你想让用户单独发布扩展包中的配置文件，而不是被强制发布扩展包中的所有资源文件。你可以通过调用扩展包服务提供者中的 <code v-pre>publishes</code> 方法给不同文件打上「标签」。例如，让我们使用扩展包服务提供者中的 <code v-pre>boot</code> 方法来定义两个发布群组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 在注册后启动服务</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">publishes</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token constant">__DIR__</span><span class="token operator">.</span><span class="token string single-quoted-string">'/../config/package.php'</span> <span class="token operator">=></span> <span class="token function">config_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'package.php'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'config'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">publishes</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token constant">__DIR__</span><span class="token operator">.</span><span class="token string single-quoted-string">'/../database/migrations/'</span> <span class="token operator">=></span> <span class="token function">database_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'migrations'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'migrations'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>现在你的用户就可以通过执行 <code v-pre>vendor:publish</code> 命令，根据定义的标签来发布不同的群组文件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan vendor<span class="token punctuation">:</span>publish <span class="token operator">--</span>tag<span class="token operator">=</span>config</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/6.x/packages/5167" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/6.x/pac...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/6.x/packages/5167" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/6.x/pac...</a></p>
</blockquote>
</div></template>


